/**
 * 定义站点地图模块
 */
(function(window, angular, undefined){
	var module = angular.module('water.apps.sw.bcAccessParam.bcAccessParamManager', ['water.entity.bcAccessParam','water.entity.bcMonitorParamDict']);
		
	module.controller('bcAccessParamManagerController',function($scope,$bcAccessParamService,$bcMonitorParamDictService){
		
		/**
		 * 设备列表数据
		 */
		$scope.paramList;
		
		/**
		 * 是否显示列表
		 */
		$scope.isShowList = true;
		/**
		 * 是否显示新建页面
		 */
		$scope.isShowAdd = false;
		/**
		 * 是否显示编辑页面
		 */
		$scope.isShowEdit = false;
		
		/**
		 * 用于新建的model
		 */
		$scope.addModel;
		/**
		 * 用于编辑的model
		 */
		$scope.editModel;
		
		/**
         * 获取传输列表的数据,使传输列表的ID和名称与access库表的名称关联
         */
		$scope.getDeviceData = function () {
			$bcMonitorParamDictService.getList().then(function (data) {
				var paramData = [];
				
                for (var i = 0; i < data.length; i++) {
                	paramData.push({
                        k: data[i].ID,
                        v: data[i].PARAM_CODE_BUS
                    });
                }
                $scope.paramData = paramData;
                $scope.paramDataModel.setData(paramData);
                $scope.editParamDataModel.setData(paramData);
            });
        };
		
		/**
		 * 打开新增窗口按钮点击事件
		 */
		$scope.onOpenCreateClick = function(){
			$scope.addModel = {};
			$scope.showAdd();
		}
		
		/**
		 * 保存按钮点击事件
		 */
		$scope.onSaveClick = function(model){
			
			for(var i=0;i<$scope.paramData.length;i++){
        		if(model.ID==$scope.paramData[i].k){
        			model.PARAM_CODE_BUS = $scope.paramData[i].v;
        		}
        	}
			
			
			$bcAccessParamService.create(model).then(function(){
				$scope.getList();
				$scope.showList();
				alert("保存成功");
			}).catch(function(error){
				alert(error);
			});
		};
		
		/**
		 * 打开编辑窗口按钮点击事件
		 */
		$scope.onOpenEditClick = function(model){
			$scope.editModel = model;
			$scope.showEdit();
		};
		
		/**
		 * 删除按钮点击事件
		 */
		$scope.onDeleteClick = function(id){
			$bcAccessParamService.deleteModel(id).then(function(){
				$scope.getList();		
				alert("删除成功");
			});
		};
		
		/**
		 * 更新按钮点击事件
		 */
		$scope.onUpdateClick = function(model){
			$bcAccessParamService.update(model).then(function(){
				$scope.getList();
				$scope.showList();
				alert("保存成功");
			}).catch(function(error){
				alert(error);
			});
		};
		
		/**
		 * 只显示列表
		 */
		$scope.showList = function(){
			$scope.isShowList = true;
			$scope.isShowAdd = false;
			$scope.isShowEdit = false;
		};
		
		/**
		 * 只显示新建页
		 */
		$scope.showAdd = function(){
			$scope.isShowList = false;
			$scope.isShowAdd = true;
		};

        /**
         * 关闭新建页
         */
        $scope.closeAdd = function(){
            $scope.isShowAdd = false;
            $scope.isShowList = true;
        };
		
		/**
		 * 只显示编辑页
		 */
		$scope.showEdit = function(){
			$scope.isShowList = false;
			$scope.isShowEdit = true;
		};

        /**
         * 关闭编辑页
         */
        $scope.closeEdit = function(){
            $scope.isShowEdit = false;
            $scope.isShowList = true;
        };
		
		$scope.getList = function(){
			$bcAccessParamService.getList().then(function(data){
				$scope.paramList = data;
			});
		};
		
		$scope._init = function(){
			$scope.getList();
			$scope.showList();
		};
		
		$scope._init();
		$scope.getDeviceData();

	});
	
}(window, angular));


